/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProfileEditionBox.java
 *
 * Created on Jan 14, 2009, 5:23:58 PM
 */
package brigal.fastbackup.gui;

import brigal.fastbackup.data.Profile;
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.filechooser.FileSystemView;
import org.jdesktop.application.Action;

/**
 *
 * @author brigal
 */
public class ProfileEditionBox extends javax.swing.JDialog {

    private Profile editedProfile;

    /** Creates new form ProfileEditionBox */
    public ProfileEditionBox(java.awt.Frame parent, boolean modal, Profile profile) {
        super(parent, modal);
        initComponents();
        getRootPane().setDefaultButton(cancelButton);

        editedProfile = profile;
        nameTextField.setText(profile.getName());
        sourceTextField.setText(profile.getSource());
        destinationTextField.setText(profile.getDestination());
    }

    public Profile getEditedProfile() {
        return editedProfile;
    }

    @Action
    public void cancelProfileEditionBox() {
        doClose();
    }

    @Action
    public void saveProfileEditionBox() {
        if (performChecks()) {
            editedProfile.setName(nameTextField.getText());
            editedProfile.setSource(sourceTextField.getText());
            editedProfile.setDestination(destinationTextField.getText());
            doClose();
        }
    }

    @Action
    public void browseSource() {
        browse(sourceTextField);
    }

    @Action
    public void browseDestination() {
        browse(destinationTextField);
    }

    private boolean performChecks() {
        File src = new File(sourceTextField.getText());
        if (!src.isDirectory()) {
            JOptionPane.showMessageDialog(this, "Source for backup must a folder.", "Source folder error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if (!src.exists()) {
            JOptionPane.showMessageDialog(this, "Source folder for backup must exist.", "Source folder error", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        File dest = new File(destinationTextField.getText());
        if (!dest.isDirectory()) {
            JOptionPane.showMessageDialog(this, "Destination for backup must a folder.", "Destination folder error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if (!dest.exists()) {
            JOptionPane.showMessageDialog(this, "Destination folder for backup must exist.", "Destination folder error", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        if (src.getAbsolutePath().equals(dest.getAbsolutePath())) {
            JOptionPane.showMessageDialog(this, "Source and destination folders must be different.", "Backup folder error", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        FileSystemView fsv = new FileSystemView() {

            @Override
            public File createNewFolder(File containingDir) throws IOException {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        };
        File srcParent = src.getParentFile();
        if (srcParent != null) {
            while (srcParent != null && !fsv.isRoot(srcParent)) {
                if (srcParent.getAbsoluteFile().equals(dest.getAbsoluteFile())) {
                    JOptionPane.showMessageDialog(this, "Source folder cannot be child of destination folder.", "Source folder error", JOptionPane.ERROR_MESSAGE);
                    return false;
                }
                srcParent = srcParent.getParentFile();
            }
        }
        File destParent = dest.getParentFile();
        if (destParent != null) {
            while (destParent != null && !fsv.isRoot(destParent)) {
                if (destParent.getAbsoluteFile().equals(src.getAbsoluteFile())) {
                    JOptionPane.showMessageDialog(this, "Destination folder cannot be child of source folder.", "Destination folder error", JOptionPane.ERROR_MESSAGE);
                    return false;
                }
                destParent = destParent.getParentFile();
            }
        }

        return true;
    }

    private void doClose() {
        setVisible(false);
        dispose();
    }

    private void browse(JTextField field) {
        JFileChooser fc = new JFileChooser(field.getText());
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        int returnVal = fc.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            field.setText(fc.getSelectedFile().getAbsolutePath());
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cancelButton = new javax.swing.JButton();
        destinationBrowseButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        nameTextField = new javax.swing.JTextField();
        nameLabel = new javax.swing.JLabel();
        sourceTextField = new javax.swing.JTextField();
        sourceLabel = new javax.swing.JLabel();
        destinationTextField = new javax.swing.JTextField();
        destinationLabel = new javax.swing.JLabel();
        sourceBrowseButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(brigal.fastbackup.gui.FastBackupApp.class).getContext().getActionMap(ProfileEditionBox.class, this);
        cancelButton.setAction(actionMap.get("cancelProfileEditionBox")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N

        destinationBrowseButton.setAction(actionMap.get("browseDestination")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(brigal.fastbackup.gui.FastBackupApp.class).getContext().getResourceMap(ProfileEditionBox.class);
        destinationBrowseButton.setText(resourceMap.getString("destinationBrowseButton.text")); // NOI18N
        destinationBrowseButton.setName("destinationBrowseButton"); // NOI18N

        saveButton.setAction(actionMap.get("saveProfileEditionBox")); // NOI18N
        saveButton.setName("saveButton"); // NOI18N

        nameTextField.setText(resourceMap.getString("nameTextField.text")); // NOI18N
        nameTextField.setName("nameTextField"); // NOI18N

        nameLabel.setText(resourceMap.getString("nameLabel.text")); // NOI18N
        nameLabel.setName("nameLabel"); // NOI18N

        sourceTextField.setText(resourceMap.getString("sourceTextField.text")); // NOI18N
        sourceTextField.setName("sourceTextField"); // NOI18N

        sourceLabel.setText(resourceMap.getString("sourceLabel.text")); // NOI18N
        sourceLabel.setName("sourceLabel"); // NOI18N

        destinationTextField.setText(resourceMap.getString("destinationTextField.text")); // NOI18N
        destinationTextField.setName("destinationTextField"); // NOI18N

        destinationLabel.setText(resourceMap.getString("destinationLabel.text")); // NOI18N
        destinationLabel.setName("destinationLabel"); // NOI18N

        sourceBrowseButton.setAction(actionMap.get("browseSource")); // NOI18N
        sourceBrowseButton.setText(resourceMap.getString("sourceBrowseButton.text")); // NOI18N
        sourceBrowseButton.setName("sourceBrowseButton"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(nameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nameTextField))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(sourceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(sourceTextField))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(sourceBrowseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 371, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(destinationLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(2, 2, 2)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(destinationTextField)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(destinationBrowseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)))))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, saveButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(destinationLabel)
                    .addComponent(sourceLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sourceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(destinationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sourceBrowseButton)
                    .addComponent(destinationBrowseButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(saveButton, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton destinationBrowseButton;
    private javax.swing.JLabel destinationLabel;
    private javax.swing.JTextField destinationTextField;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JButton saveButton;
    private javax.swing.JButton sourceBrowseButton;
    private javax.swing.JLabel sourceLabel;
    private javax.swing.JTextField sourceTextField;
    // End of variables declaration//GEN-END:variables
}
